{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# redis\n",
    "pip install redis python-redis\n",
    "ipconfig\n",
    "docker run --name redislearn -p 6379:6379 -d redis:latest\n",
    "docker run --name redisinsightlearn -p 8001:8001 -d redislabs/redisinsight:1.14.0\n",
    "docker exec -it redislearn bash    \n",
    "redis-cli -h host -p port -a password\n",
    "\n",
    "\n",
    "dbsize 查看当前库中key的数量\n",
    "set/del/exists/ttl key\n",
    "\n",
    "expire：用于设置 key 的过期时间。key 过期后将不再可用。格式：expire key seconds\n",
    "expireat：用于以 UNIX 时间戳(unix timestamp)格式设置 key 的过期时间。key 过期后将不再可用。格式：expireat key timestamp\n",
    "pexpireat：用于设置 key 的过期时间，已毫秒计算。key 过期后将不再可用。格式：pexpireat key milliseconds-timestamp\n",
    "persist：用于移除给定 key 的过期时间，使得 key 永不过期。格式：persist key\n",
    "\n",
    "pttl：以毫秒为单位返回 key 的剩余过期时间。格式：pttl key\n",
    "ttl：以秒为单位返回 key 的剩余过期时间。格式：ttl key\n",
    "move 将当前数据库的key移动到数据库db当中。格式：move key db\n",
    "\n",
    "rename 修改key的名称，将key改名为newkey。格式：rename key newkey\n",
    "renamenx 仅当newkey不存在时，将key改名为newkey。格式：renamenx key newkey\n",
    "type 返回key所存储的值的类型。格式：type key\n",
    "\n",
    "覆盖规则\n",
    "NX: 只在键不存在时执行；等同于 setnx key value.\n",
    "XX: 只在键存在时执行；\n",
    "SETNX: Set If Not Exists 的简写。为字符串键设置值，当key不存在时，设置成功。基本语法如下：setnx key value。\n",
    "SETEX: 为字符串键设置值的同时，并设置过期时间。SETEX命令的基本语法如下。 setex key seconds value 。\n",
    "MSET 一次为多个字符串键设置值。\n",
    "MGET 一次获取多个字符串键的值。\n",
    "\n",
    "STRLEN 获取字符串值的字节长度。基本语法如下： strlen key\n",
    "INCR/DECR 将key中存储的整数值加1，并返回加1操作之后的值。incr key\n",
    "INCRBY/DECRBY 将key所存储的整数值加上指定的整数增量，并返回执行加法操作之后的值。incrby key increment\n",
    "\n",
    "hset/hget 设置哈希表中一个或多个字段及值。基本用法如下：HSET key field value [field value …]\n",
    "hmget 同时获取一个key的多个field的值。\n",
    "hkeys 作用：获取key对应的所有的field 格式：hkeys key\n",
    "hgetall 作用：获取一个key下所有field和value。格式：hgetall key\n",
    "hlen 作用：返回指定key的field的数量。\n",
    "hdel 作用：删除key中一个或多个filed的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Redis<ConnectionPool<Connection<host=192.168.169.1,port=6379,db=0>>>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import redis\n",
    " \n",
    "r = redis.Redis(host='192.168.169.1', port=6379, decode_responses=True, max_connections=None)\n",
    "print(r)\n",
    "r.set('foo', 'Bar')\n",
    "print(r.get('foo')) \n",
    "# decode_responses=False: b'Bar' <class 'bytes'>\n",
    "# decode_responses=True:    Bar <class 'str'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = r.set('kk1', 'vv1', ex=30)\n",
    "v1 = r.get('kk1')\n",
    "print(res)  \n",
    "print(v1)  \n",
    "\n",
    "res = r.set('kk2', 'vv字符串', ex=30)\n",
    "v2 = r.get('kk2')\n",
    "print(v2)  \n",
    "r.hset(\"hash1\", \"k1\", \"v1\")\n",
    "r.hset(\"hash1\", \"k2\", \"v2\")\n",
    "\n",
    "print(r.hkeys(\"hash1\"))  # 取hash中所有的key\n",
    "print(r.hget(\"hash1\", \"k5\"))  # 单个取hash的key对应的值。不存在返回None\n",
    "print(r.hmget(\"hash1\", \"k1\", \"k2\"))  # 多个取hash的key对应的值\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python39",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
